package com.mashibing.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 构建RabbitMQ的Connection对象
 * @author zjw
 */
public class RabbitMQConnectionUtil {

    public static String host = "192.168.11.11";
    public static int port = 5672;
    public static String username = "guest";
    public static String password = "guest";
    public static String virtualHost = "/";

    public static Connection getConnection(){
        //1. 构建Connection工厂
        ConnectionFactory factory = new ConnectionFactory();

        //2. 设置连接信息
        factory.setHost(host);
        factory.setPort(port);
        factory.setUsername(username);
        factory.setPassword(password);
        factory.setVirtualHost(virtualHost);

        //3. 构建连接对象并返回
        try {
            Connection connection = factory.newConnection();
            return connection;
        } catch (IOException | TimeoutException e) {
            throw new RuntimeException("连接RabbitMQ失败！");
        }
    }

}
